package com.zksk.admin.domain.system.vo;

import com.zksk.mbg.admin.domain.SysDictArea;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 地区 封装树类
 * </p>
 *
 * @author Zksk Acmen
 * @since 2022-12-08
 */
public class AreaTreeVo {

    private List<SysDictArea> children;
    private List<AreaVo> list = new ArrayList<>();

    public List<AreaVo> areaList(List<SysDictArea> children){
        this.children = children;
        for (SysDictArea item : children) {
            AreaVo areaVo = new AreaVo();
            if(item.getParentCode() == 0L){
                BeanUtils.copyProperties(item, areaVo);
                areaVo.setChildren(areaChildren(item.getAreaCode()));
                list.add(areaVo);
            }
        }
        return list;
    }
    private List<AreaVo> areaChildren(Long areaCode) {
        List<AreaVo> lists = new ArrayList<>();
        for (SysDictArea item : children) {
            AreaVo areaVo = new AreaVo();
            if (item.getParentCode().equals(areaCode)) {
                BeanUtils.copyProperties(item, areaVo);
                areaVo.setChildren(areaChildren(item.getAreaCode()));
                lists.add(areaVo);
            }
        }
        return lists;
    }

    public List<AreaVo> areaListByCurrent(List<SysDictArea> children, Long areaCode){
        this.children = children;
        for (SysDictArea item : children) {
            AreaVo areaVo = new AreaVo();
            if(item.getAreaCode().equals(areaCode)){
                BeanUtils.copyProperties(item, areaVo);
                areaVo.setChildren(areaChildren(item.getParentCode()));
                list.add(areaVo);
            }
        }
        return list;
    }

}
